package common.liusany.admin.utils;

import java.io.File;
import java.io.FileOutputStream;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import common.liusany.admin.bean.Setting;

/**
 * 工具类 - 系统配置
 * 
 * @author luoli
 */

public class SettingUtil {
	
	public static final String SETTING_FILE_NAME = "emails-config.xml";// 系统配置文件名称
	public static final String SETTING_CACHE_KEY = "setting";// settging缓存Key

	/**
	 * 获取系统配置信息
	 * 
	 * @return Setting对象
	 */
	public static Setting getSetting() {
		Setting setting = (Setting) EhCacheConfigUtil.getFromCache(SETTING_CACHE_KEY);
		if (setting != null) {
			return setting;
		}
		File settingFile = null;
		Document document = null;
		try {
			String settingFilePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath() + SETTING_FILE_NAME;
			settingFile = new File(settingFilePath);
			SAXReader saxReader = new SAXReader();
			document = saxReader.read(settingFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Node smtpFromMailNode = document.selectSingleNode("/festore/setting/smtpFromMail");
		Node smtpHostNode = document.selectSingleNode("/festore/setting/smtpHost");
		Node smtpPortNode = document.selectSingleNode("/festore/setting/smtpPort");
		Node smtpUsernameNode = document.selectSingleNode("/festore/setting/smtpUsername");
		Node smtpPasswordNode = document.selectSingleNode("/festore/setting/smtpPassword");
		Node subjectNode = document.selectSingleNode("/festore/setting/subject");
		
		setting = new Setting();
		setting.setSmtpFromMail(smtpFromMailNode.getText());
		setting.setSmtpHost(smtpHostNode.getText());
		setting.setSmtpPort(Integer.parseInt(smtpPortNode.getText()));
		setting.setSmtpUsername(smtpUsernameNode.getText());
		setting.setSmtpPassword(smtpPasswordNode.getText());
		setting.setSubject(subjectNode.getText());
		EhCacheConfigUtil.putInCache(SETTING_CACHE_KEY, setting);
		return setting;
	}
	/**
	 * 更新系统配置信息
	 * 
	 * @param setting
	 *          SystemConfig对象
	 */
	public static void update(Setting setting) {
		File settingFile = null;
		Document document = null;
		try {
			String settingFilePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath() + SETTING_FILE_NAME;
			settingFile = new File(settingFilePath);
			SAXReader saxReader = new SAXReader();
			document = saxReader.read(settingFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Element rootElement = document.getRootElement();
		Element settingElement = rootElement.element("setting");
		
		Node smtpFromMailNode = document.selectSingleNode("/festore/setting/smtpFromMail");
		Node smtpHostNode = document.selectSingleNode("/festore/setting/smtpHost");
		Node smtpPortNode = document.selectSingleNode("/festore/setting/smtpPort");
		Node smtpUsernameNode = document.selectSingleNode("/festore/setting/smtpUsername");
		Node smtpPasswordNode = document.selectSingleNode("/festore/setting/smtpPassword");
		Node subjectNode = document.selectSingleNode("/festore/setting/subject");
		
		if(smtpFromMailNode == null)smtpFromMailNode = settingElement.addElement("smtpFromMail");
		if(smtpHostNode == null)smtpHostNode = settingElement.addElement("smtpHost");
		if(smtpPortNode == null)smtpPortNode = settingElement.addElement("smtpPort");
		if(smtpUsernameNode == null)smtpUsernameNode = settingElement.addElement("smtpUsername");
		if(smtpPasswordNode == null)smtpPasswordNode = settingElement.addElement("smtpPassword");
		if(subjectNode == null)subjectNode = settingElement.addElement("subject");
		
		
		smtpFromMailNode.setText(setting.getSmtpFromMail());
		smtpHostNode.setText(setting.getSmtpHost());
		smtpPortNode.setText(String.valueOf(setting.getSmtpPort()));
		smtpUsernameNode.setText(String.valueOf(setting.getSmtpUsername()));
		smtpPasswordNode.setText(setting.getSmtpPassword());
		subjectNode.setText(String.valueOf(setting.getSubject()));
		
		try {
			OutputFormat outputFormat = OutputFormat.createPrettyPrint();// 设置XML文档输出格式
			outputFormat.setEncoding("UTF-8");// 设置XML文档的编码类型
			outputFormat.setIndent(true);// 设置是否缩进
			outputFormat.setIndent("	");// 以TAB方式实现缩进
			outputFormat.setNewlines(true);// 设置是否换行
			XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(settingFile), outputFormat);
			xmlWriter.write(document);
			xmlWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		EhCacheConfigUtil.flush();
	}
	
	/**
	 * 刷新系统配置信息
	 * 
	 */
	public void flush() {
		EhCacheConfigUtil.flush();
	}
}